package com.movie.api.controller;

import cn.hutool.core.bean.BeanUtil;
import com.movie.api.model.dto.EvaluateRankDto;
import com.movie.api.model.dto.PageDto;
import com.movie.api.model.entity.Film;
import com.movie.api.model.vo.FilmInfoAndStarVO;
import com.movie.api.model.vo.FilmVO;
import com.movie.api.service.FilmEvaluateService;
import com.movie.api.service.FilmIsVipService;
import com.movie.api.model.vo.FilmInfoVO;
import com.movie.api.model.vo.PageResultVO;
import com.movie.api.service.FilmService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
@Api(tags = "电影接口")
@RequestMapping("/api/film")
@Slf4j
public class FilmController {
    @Resource
    private FilmEvaluateService filmEvaluateService;
    @Resource
    private FilmIsVipService filmIsVipService;
    @Resource
    private FilmService filmService;

    @PostMapping("")
    @ApiOperation(value = "保存电影")
    public void save(@RequestBody Film film) {
        filmService.save(film);
    }

    @GetMapping("")
    @ApiOperation("列出所有电影")
    public List<Film> list(String region, String type) {
        if (region != null && type != null) {
            return filmService.findByRegionAndType(region, type);
        }
        return filmService.findAll();
    }

    @GetMapping("/listVO")
    @ApiOperation("列出所有电影")
    public List<FilmVO> listVO(String region, String type) {
        if (region != null && type != null) {
            List<Film> byRegionAndType = filmService.findByRegionAndType(region, type);
            return getFilmVOS(byRegionAndType);
        }
        List<Film> films = filmService.findAll();
        return getFilmVOS(films);
    }

    private List<FilmVO> getFilmVOS(List<Film> films) {
        List<FilmVO> filmVOList = new ArrayList<>();
        for (Film film : films) {
            FilmVO filmVO = new FilmVO();
            BeanUtils.copyProperties(film, filmVO);
            filmVO.setVip(filmIsVipService.isVip(film.getId()));
            filmVOList.add(filmVO);
        }
        log.info("filmVOList:{}", filmVOList);
        return filmVOList;
    }

    @GetMapping("/name/{name}")
    @ApiOperation("搜索电影")
    public List<Film> search(@PathVariable String name) {
        return filmService.findLikeName(name);
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查找电影")
    public FilmInfoVO findById(@PathVariable String id) {
        return filmService.findById(id);
    }

    @GetMapping("/filminfoandstarVO/{id}")
    @ApiOperation(value = "根据id查找电影")
    public FilmInfoAndStarVO findFilmInfoAndStarVOById(@PathVariable String id) {
        FilmInfoVO byId = filmService.findById(id);
        FilmInfoAndStarVO filmInfoAndStarVO = new FilmInfoAndStarVO();
        BeanUtils.copyProperties(byId, filmInfoAndStarVO);
        log.info("filmInfoAndStarVO:{}", filmInfoAndStarVO);
        String avgStar = filmEvaluateService.getAvgStar(id);
        log.info("Controller返回AVG:{}", avgStar);
        filmInfoAndStarVO.setStar(avgStar);
        return filmInfoAndStarVO;
    }

    @PutMapping("")
    @ApiOperation(value = "更新电影")
    public void update(@RequestBody Film film) {
        filmService.update(film);
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "根据id删除电影")
    public void deleteById(@PathVariable String id) {
        filmService.deleteById(id);
    }


    @GetMapping("/getFilmByTime")
    @ApiOperation(value = "根据数字类型来按周、月、全部播放排行")
    public List<Film> getFilmByTime(String time) {
        return filmService.getFilmByTime(time);
    }

    @GetMapping("/wyc")
    public void viewAt(Integer filmId) {
        filmService.viewAt(filmId);
    }

    @GetMapping("/page")
    public List<FilmInfoVO> page(String selectedOption, String searchText) {
        return filmService.page(selectedOption, searchText);
    }

    @GetMapping("/getFilmByPage")
    public PageResultVO pageList(PageDto pageDto) {
        return filmService.pageList(pageDto);
    }

    @GetMapping("/getFilmEvaluateRankByPage")
    public PageResultVO getFilmByComment(EvaluateRankDto evaluateRankDto) {
        return filmService.getEvaluateRank(evaluateRankDto);
    }
}
